<?php

class _router extends base
{
    //要先删除 router/cache.php 再执行此脚本
    public function make_cache()
    {
        $files = scandir(ROUTERPATH);		
        sort($files);
        
        $arr = [];
        $arr[] = '<?php'.PHP_EOL.'$GLOBALS["router"]["cache"] = [';
        foreach ($files as $file) {
			$i = pathinfo($file);
			$name = $i['filename'];
			$ext = $i['extension'];
			if ($ext == 'php' && $name !== 'cache') {
				include(ROUTERPATH.$file);
                $str = var_export($GLOBALS['router'][$name], true);
                $str = str_replace('\\\\', '\\', $str);
				$arr[] = "'$name' => " . $str . ','.PHP_EOL;
			}
		}

        $arr[] = '];';
        file_put_contents(ROUTERPATH.'cache.php', implode(PHP_EOL, $arr));
        echo 'ok';
    }
}